﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TA.JSON
{
	/// <summary>
	/// Serializes to JSON output.
	/// </summary>
	public interface IJsonSerializable
	{
		/// <summary>
		/// True if any property of this object is dirty (different from the default value - needs serialization).
		/// </summary>
		bool IsDirty { get; }
		/// <summary>
		/// Serialize this object, only outputting dirty properties. Outputs start/end elements.
		/// </summary>
		/// <param name="jw"></param>
		void Serialize(JsonWriter jw);
		/// <summary>
		/// Serialize this object, only outputting dirty properties. Doesn't output start/end elements.
		/// </summary>
		/// <param name="jw"></param>
		void SerializeContents(JsonWriter jw);
	}
	/// <summary>
	/// Extension methods for IJsonSerializable.
	/// </summary>
	public static class _IJsonSerializableExtensions
	{
		/// <summary>
		/// Performs common tasks necessary when serializing an object.
		/// </summary>
		/// <param name="obj"></param>
		/// <param name="jw"></param>
		public static void SerializeObject(this IJsonSerializable obj, JsonWriter jw)
		{
			jw.StartObject();

			obj.SerializeContents(jw);

			jw.EndObject();
		}
		/// <summary>
		/// Performs common tasks necessary when serializing an array.
		/// </summary>
		/// <param name="obj"></param>
		/// <param name="jw"></param>
		public static void SerializeArray(this IJsonSerializable obj, JsonWriter jw)
		{
			jw.StartArray();
			obj.SerializeContents(jw);
			jw.EndArray();
		}
	}
}
